import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/el_refresh.dart';
import 'package:wallet_utils/log_util.dart';
import 'package:wallet_utils/report_widget/base/pager/refresh_pager_utils.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/*封装项目中大多数情况下的按钮使用*/
/*没有对外暴露child,不让去重写child控件,防止自定义TextStyle*/
Widget htButton({
  Widget? child, //自定义child
  String? title, //title为按钮文字
  double? width, //自定义宽度
  double? height, //自定义高度
  double? fontSize, //自定义文字大小
  FontWeight? fontWeight, //自定义文字字体
  Color? backgroundColor, //自定义背景颜色
  Color? disabledColor, //自定义不可点击时背景颜色
  Color? foregroundColor, //自定义文字颜色
  VoidCallback? onPressed, //自定义点击回调
  double elevation = 0, //自定义阴影,系统默认是2
  bool enable = true, //是否禁用按钮,附自动切换背景色
  double borderWidth = 0, //自定义边框大小
  Color borderColor = Colors.transparent, //自定义边框颜色
  ButtonStyle? style, //自定义style
  // double? radius, //自定义圆角大小,如果不设置,默认自带的圆角效果
}) {
  /*包一层的好处是不允许外界直接调用Button的各种属性*/
  var buttonStyle = ButtonStyle(
    textStyle: MaterialStateProperty.all(
      TextStyle(
        fontSize: fontSize ?? 14.sp, //功能按钮类UI图默认都是14
        fontWeight: fontWeight ?? FontWeight.w500, //功能按钮类UI都是Medium
      ),
    ),
    fixedSize: MaterialStateProperty.all(
      Size(
        width ?? 0, //自定义宽度
        height ?? 44.w, //自定义高度
      ),
    ),
    backgroundColor: enable == true //不在textStyle中设置,便于buttonTheme统一修改
        ? MaterialStateProperty.all(
            backgroundColor ?? ColorManager.colorF65247, //主题红色
          )
        : MaterialStateProperty.all(
            disabledColor ?? ColorManager.colorFBB9B5, //主题红色不可点击状态
          ),
    overlayColor: MaterialStateProperty.all(
      backgroundColor ?? ColorManager.colorF65247, //主题红色不可点击状态
    ),
    foregroundColor: MaterialStateProperty.all(
      foregroundColor ?? Colors.white, //不在textStyle中设置,便于buttonTheme统一修改
    ),
    elevation: MaterialStateProperty.all(
      elevation, //默认不显示,设为0;如果需要显示,系统默认是2
    ),
    side: MaterialStateProperty.all(
      BorderSide(
        width: borderWidth, //边框大小
        color: borderColor, //边框颜色
        style: BorderStyle.solid, //显示格式
      ),
    ),
    //todo 待完善
    //暂时找不到好的button圆角的方式,所以完全用系统自带的效果
    // shape: MaterialStateProperty.all(
    //     /*这种方式可以自定义圆角大小,但是不平滑,是棱形*/
    //     BeveledRectangleBorder(
    //   borderRadius: BorderRadius.circular(radius ?? 0), //圆角弧度
    // )),

    // ElevatedButton去除内置padding,否则text可能会转行
    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
    minimumSize: MaterialStateProperty.all(const Size(0, 0)),
    padding: MaterialStateProperty.all(EdgeInsets.zero),
  );
  return ElevatedButton(
    style: style ?? buttonStyle,
    onPressed: () {
      if (onPressed != null && enable == true) {
        onPressed();
      } //禁用后点击事件无法触发
    },
    child: child ?? Text(title ?? ''),
  );
}

/*封装的底部容器*/
Widget htBottomBar({
  Widget? child,
}) {
  return Container(
    decoration: BoxDecoration(
      color: Colors.white,
      boxShadow: [
        BoxShadow(
          color: ColorManager.color000000.withOpacity(0.11),
          offset: const Offset(0.0, 0.0), //阴影xy轴偏移量
          blurRadius: 4.0, //阴影模糊程度
          spreadRadius: 0.0, //阴影扩散程度
        ),
      ],
    ),
    padding:
        EdgeInsets.only(bottom: max(0.h, ScreenUtil().bottomBarHeight)), //底部的距离
    child: Padding(
      padding:
          EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.h), //子视图上下左右的距离
      child: child ??
          Container(
            height: 40,
          ),
    ),
  );
}

/*封装的无数据组件*/
Widget htEmptyDataWidget({
  String? title,
  String? image,
}) {
  return Container(
    color: Colors.white,
    alignment: Alignment.center,
    padding: EdgeInsets.only(top: 100.w),
    child: Column(
      children: [
        Image.asset(
          image ?? 'images/online_mall/send_stamps/send_stamps_nodata.png',
          width: 190.w,
          height: 100.w,
        ),
        SizedBox(
          height: 20.w,
        ),
        Text(
          title ?? '暂无数据',
          style: TextStyleUtils.style999999(),
        ),
      ],
    ),
  );
}

/*封装的图片显示控件*/
Widget HtImage({
  required String? url, //图片的url
  double? width, //图片宽度
  double? height, //图片高度
  BoxFit? fit = BoxFit.cover, //默认图片的拉伸方式
  double radius = 2, //默认圆角
  String? placeholder, //默认显示图
  String? errImage, //默认请求错误图
  bool? icon, //如果头像显示使用,可自动更换默认图
}) {
  return Container(
    child: CachedNetworkImage(
      imageUrl: url ?? '',
      width: width,
      height: height,
      imageBuilder: (context, imageProvider) => Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(radius.r),
          image: DecorationImage(
            image: imageProvider,
            fit: fit,
          ),
        ),
      ),
      placeholder: (context, url) => Image.asset(
        placeholder ??
            (icon == true
                ? 'images/default_icon.webp'
                : 'images/default_placeholder.webp'),
        height: width,
        width: height,
        fit: fit,
      ),
      errorWidget: (context, url, error) => Image.asset(
        errImage ??
            (icon == true
                ? 'images/default_icon.webp'
                : 'images/default_placeholder.webp'),
        height: width,
        width: height,
        fit: fit,
      ),
    ),
  );
}

//封装项目需要用到的下拉刷新 上拉加载
Widget htRefreshWidget({
  Widget? child, //需要用到刷新的控件
  RefreshController? refreshController, //绑定的刷新控制器
  Future<dynamic>? loadFirstPage, // 下拉刷新方法
  Future<dynamic>? loadMorePage, //上拉加载方法
  bool? isLoading, //是否显示菊花
}) {
  return Container(
    child: elRefresh(
      refreshController: refreshController,
      onRefresh: () async {
        LogUtils.d("htRefresh下拉刷新");
        loadFirstPage;
        refreshController?.refreshCompleted();
      },
      onLoading: () async {
        LogUtils.d("htRefresh上拉加载");
        loadMorePage;
        refreshController?.loadComplete();
      },
      child: loadingProgressWithColor(
        isLoading ?? false,
        child: child,
      ),
    ),
  );
}
